package com.mida.dms.knw.controller;

import com.mida.dms.knw.entity.KnwCompositeInstance;
import com.mida.dms.knw.entity.vo.KnwCompositeInstanceDto;
import com.mida.dms.knw.service.KnwCompositeInstanceService;
import com.mida.dms.knw.vo.KnwCompositeInstanceVo;
import com.mida.dms.knw.vo.KnwCompositeTmpVo;
import com.mida.dms.knw.vo.PageResultVo;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.mida.tool.common.result.BaseResult.buildListSuccess;

/**
 * @author ：dj
 * @description：复合实例
 */
@RestController
@RequestMapping("/knwCompositeInstance")
@CrossOrigin
@Slf4j
public class KnwCompositeInstanceController {

    @Autowired
    private KnwCompositeInstanceService knwCompositeInstanceService;

    /**
     * 添加复合实例
     * @param knwCompositeInstanceVo
     * @return
     */
    @PostMapping(value = "/addKnwCompositeInstance",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult addKnwCompositeInstance(
            @RequestBody KnwCompositeInstanceVo knwCompositeInstanceVo) {

        ActionResult result = new ActionResult();
        try {
             result = knwCompositeInstanceService.add(knwCompositeInstanceVo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("添加模版基本信息失败");
            return result;
        }
        return result;
    }

    /**
     * 修改复合实例数据
     *
     * @param knwCompositeInstanceVo
     * @return
     */
    @PostMapping(value = "/updateKnwCompositeInstance",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult updateKnwCompositeInstance(
            @RequestBody KnwCompositeInstanceVo knwCompositeInstanceVo) {
        ActionResult result = new ActionResult();
        try {
            result = knwCompositeInstanceService.update(knwCompositeInstanceVo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("修改模版数据失败");
            return result;
        }
        return result;
    }

    /**
     * 删除复合实例数据
     *
     * @param knwCompositeInstanceVo 对象接收前端传入的json
     * @return
     */
    @PostMapping(value = "/deleteKnwCompositeInstance",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult deleteKnwCompositeInstance(@RequestBody KnwCompositeInstanceVo knwCompositeInstanceVo) {
        ActionResult result = new ActionResult();
        try {
            result = knwCompositeInstanceService.delete(knwCompositeInstanceVo);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("删除模版数据失败");
            return result;
        }
        return result;
    }

    /**
     * 查询复合实例分页列表处理
     *
     * @param knwCompositeInstanceVo 对象接收前端传入的json
     * @return
     */
    @PostMapping(value = "/getKnwCompositeInstancePage",produces = {"application/json;charset=UTF-8"})
    public ListResult<PageResultVo<KnwCompositeInstanceDto>> getKnwCompositeInstancePage(
            @RequestBody KnwCompositeInstanceVo knwCompositeInstanceVo) {
        ListResult result = new ListResult();
        PageResultVo<KnwCompositeInstanceDto> data =
                knwCompositeInstanceService.getKnwCompositeInstancePage(knwCompositeInstanceVo);
        buildListSuccess(result, data);
        return result;
    }

    /**
     * 5、排序复合实例
     *
     * @param
     * @return
     */
    @PostMapping(value = "/updateSort",consumes = {"application/json"})
    public ActionResult updateSort(@RequestBody List<KnwCompositeInstanceVo> knwCompositeInstanceVoList) {
        ActionResult result = new ActionResult();
        try {
            result = knwCompositeInstanceService.updateSort(knwCompositeInstanceVoList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("排序失败");
            return result;
        }
        return result;
    }

    /**
     * 查询复合实例表处理
     *
     * @param knwCompositeInstanceVo 对象接收前端传入的json
     * @return
     */
    @PostMapping(value = "/getKnwCompositeInstanceList",consumes = {"application/json"})
    ListResult<List<KnwCompositeInstanceDto>> getKnwCompositeInstanceList(
            @RequestBody KnwCompositeInstanceVo knwCompositeInstanceVo) {
        ListResult result = new ListResult();
        List<KnwCompositeInstanceDto> list =
                knwCompositeInstanceService.getKnwCompositeInstanceList(knwCompositeInstanceVo);
        buildListSuccess(result, list);
        return result;
    }

    /**
     * 启用与禁用复合实例数据
     *
     * @param knwCompositeInstanceVo
     * @return
     */
    @PostMapping(value = "/enableKnwCompositeInstance",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult enableKnwCompositeInstance(@RequestBody KnwCompositeInstanceVo knwCompositeInstanceVo) {
        ActionResult result = new ActionResult();
        try {
            result = knwCompositeInstanceService.enableKnwCompositeInstance(knwCompositeInstanceVo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(result.getStatus());
            result.setMessage(result.getMessage());
            return result;
        }
        return result;
    }

    /**
     * 添加复合
     * @param vo
     * @return
     */
    @PostMapping(value = "/addKnwComposite",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult addKnwComposite(@RequestBody KnwCompositeTmpVo vo) {

        ActionResult result = new ActionResult();

        try {
            result = knwCompositeInstanceService.addKnwComposite(vo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("添加复合失败");
            return result;
        }
        return result;
    }
}
